import grace from "../../utils/grace.js"
grace.page({
      data: {
            tab_index: 0,
            pageSize: 10,
            tab: [{
                        total: 0,
                        list: [],
                        hasMore: false,
                        pageCurrent: 1
                  },
                  {
                        total: 0,
                        list: [],
                        hasMore: false,
                        pageCurrent: 1
                  },
            ]
      },
      onLoad: function(options) {
            if (options[0][0]['tab']) {
                  this.$data.tab_index = options[0][0]['tab'];
            }
            this.loadData();
      },
      loadData: function() {
            let uri_arr = ["api/goods_menu/list", "api/doingit/list"];
            let current_tab = this.$data.tab[this.$data.tab_index];
            let params = {
                  pageSize: this.$data.pageSize,
                  pageCurrent: current_tab.pageCurrent
            };
            let current_url = uri_arr[this.$data.tab_index];
            this.$http.get(current_url, params).then((res) => {
                  wx.stopPullDownRefresh();
                  let hasMore = res.list.length >= this.$data.pageSize;
                  const data = current_tab.pageCurrent == 1 ? res.list : current_tab.list.concat(res.list);
                  this.$data.tab[this.$data.tab_index]['total'] = res.total;
                  this.$data.tab[this.$data.tab_index]['list'] = data;
                  this.$data.tab[this.$data.tab_index]['hasMore'] = hasMore;
            });
      },
      changeTab: function(e) {
            console.log(e);
            let index = e.target.dataset.index;
            this.$data.tab_index = index;
            let current_tab = this.$data.tab[this.$data.tab_index];
            if (current_tab.list.length == 0) {
                  this.loadData();
            }
      },
      openDetail: function(e) {
            let menu = e.currentTarget.dataset.menu;
            wx.navigateTo({
                  url: '../menu_recommend/menu_recommend?menu_id=' + menu,
            })
      },
      openDoing: function(e) {
            let doid = e.currentTarget.dataset.doid;
            wx.navigateTo({
                  url: '../doing/doing?do_id=' + doid,
            })
      },
      praiseMenu: function(e) {
            let menu = e.currentTarget.dataset.menu;
            let index = e.currentTarget.dataset.idx;
            this.$http.post('api/goods_menu/praise', {
                  menu_id: menu
            }).then((res) => {
                  let data = this.$data.tab[this.$data.tab_index]['list'];
                  if (index < data.length) {
                        data[index]['praise_count'] = parseInt(data[index]['praise_count']) + 1;
                        this.$data.tab[this.$data.tab_index]['list'] = data;
                  }
            });
      },
      praiseDoing: function(e) {
            let doing_id = e.currentTarget.dataset.doid;
            let index = e.currentTarget.dataset.idx;
            this.$http.post('api/doingit/praise', {
                  doing_id: doing_id
            }).then((res) => {
                  let data = this.$data.tab[this.$data.tab_index]['list'];
                  if (index < data.length) {
                        data[index]['praise_count'] = parseInt(data[index]['praise_count']) + 1;
                        this.$data.tab[this.$data.tab_index]['list'] = data;
                  }
            });
      },
      onPullDownRefresh: function() {
            this.$data.tab[this.$data.tab_index]['pageCurrent'] = 1;
            this.loadData();
      },
      onReachBottom: function() {
            let current_tab = this.$data.tab[this.$data.tab_index];
            if (current_tab.hasMore) {
                  this.$data.tab[this.$data.tab_index]['pageCurrent'] = current_tab.pageCurrent + 1;
                  this.loadData();
            }
      },
      $onBackData(data) {
            if (data && data.isUpdate) {
                  wx.startPullDownRefresh();
            }
      },
      openDoingup: function() {
            wx.navigateTo({
                  url: '../doing_up/doing_up',
            })
      }
})